package com.legendframework.core.cmd.paramer.converter.impl;

import com.legendframework.core.ioc.IParameter;
import com.legendframework.core.ioc.annotation.Component;
import com.legendframework.core.cmd.exception.ParameterCastException;
import com.legendframework.core.cmd.paramer.converter.ParamerConverter;
import org.bukkit.command.CommandSender;

import java.lang.reflect.Field;
import java.lang.reflect.Parameter;

@Component
public class BooleanParamerConverter implements ParamerConverter<Boolean> {

    @Override
    public boolean check(Class<?> cls) {
        return boolean.class == cls || Boolean.class == cls;
    }

    @Override
    public Boolean castParameter(CommandSender sender, String message, Class<Boolean> type, IParameter parameter) {
        if (!"true".equalsIgnoreCase(message) && !"false".equalsIgnoreCase(message)){
            throw new ParameterCastException("转化布尔值失败：'"+message+"' ,  预期 true | false");
        }
        return Boolean.parseBoolean(message);
    }
}
